﻿using System;
using System.Collections.ObjectModel;

namespace Com.Lancetinc.Evs.Helpers
{
    public class SortedObservableCollection<T> : ObservableCollection<T> where T : IComparable<T>
    {
        public SortedObservableCollection()
        {
        }

        public new void Add(T item)
        {
            var idx = this.Count;
            while (idx-- > 0)
            {
                int comparedResult = item.CompareTo(this[idx]);
                if (comparedResult >= 0)
                {
                    base.InsertItem(idx + 1, item);
                    return;
                }
            }
            base.InsertItem(0, item);
        }
    }
}
